<?php
namespace classlib\imsdk;
class improc {
	public static $skey = "123456";
	public static $szip = "10.0.2.68";
	public static $port = 33133;
	public static $socket;
	
	/**
	 * 参数资料加密信息数据
	 * @param string $sdata
	 * @param string $vkey
	 * @return encryptdata
	 */
	public static function encrypt($sdata, $vkey) {
		$len  = strlen($vkey);
		$slen = strlen($sdata);
		for($i=0; $i < $slen; $i++) {
			$sdata[$i] = chr(ord($sdata[$i]) ^ ord($vkey[$i%$len]));
		}
		$sdata = base64_encode($sdata);
		return $sdata;
	}
	
	/**
	 * 参数数据信息解码
	 * @param string $sdata
	 * @param string $vkey
	 * @return decryptdata
	 */
	public static function decrypt($sdata, $vkey) {
		$sdata = base64_decode($sdata);
		$len   = strlen($vkey);
		$slen  = strlen($sdata);
		for($i=0; $i < $slen; $i++) {
			$sdata[$i] = chr(ord($sdata[$i]) ^ ord($vkey[$i%$len]));
		}
		return $sdata;
	}
	
	/**
	 * 接口请求初始化
	 * @param string $ip
	 * @param int    $port
	 * @param int    $chind
	 */
	public static function __init__($ip, $port) {
		self::$szip   = $ip;
		self::$port   = $port;	
		self::$socket = new socket(self::$szip, self::$port);
		if (!self::$socket->connect()) {
			return false;
		}
		return true;
	}
	
	/**
	 * 会员信息注册
	 * @param int $uid
	 * @param string $dev
	 * @param string $anoy
	 * @param string $cast
	 * @param string $vkey
	 * @return array code=>0, packdata
	 */
	public static function register($uid, $dev, $anoy, $cast, $vkey) {
		$proc   = new protocal(self::$socket);
		$pkdata = array('key'=>self::$skey, 'uin'=>$uid, 'anoy'=>$anoy,
			'cast'=>$cast, 'dev'=>$dev, 'vkey'=>$vkey);
		$pkdata = json_encode($pkdata);
		$nsize  = $proc->package(1, 1, $pkdata);
		if ($nsize != 0) {
			return array('code'=>1, 'packdata'=>'socket send error!');
		}
		return $proc->parse();
	}
	
	/**
	 * 会员房间权限设置
	 * @param int $uid
	 * @param int $rin
	 * @param int $access
	 * @return array code=>0, packdata
	 */
	public static function access($uid, $rin, $access) {
		$proc   = new protocal(self::$socket);
		$pkdata = array('key'=>self::$skey, 'uin'=>$uid, 'rin'=>$rin, 'access'=>$access);
		$pkdata = json_encode($pkdata);
		$nsize  = $proc->package(1, 8, $pkdata);
		if ($nsize != 0) {
			return array('code'=>1, 'packdata'=>'socket send error!');
		}
		return $proc->parse();
	}
	
	/**
	 * 会员登录
	 * @param int $uid
	 * @param string $dev
	 * @param string $vkey
	 * @return array code=>0, packdata
	 */
	public static function login($uid, $dev, $vkey) {
		$proc   = new protocal(self::$socket);
		$pkdata = array('uin'=>$uid, 'dev'=>$dev, 'vkey'=>$vkey);
		$pkdata = json_encode($pkdata);
		$nsize  = $proc->package(1, 2, $pkdata);
		if ($nsize != 0) {
			return array('code'=>1, 'packdata'=>'socket send error!');
		}
		return $proc->parse();
	}
	
	/**
	 * 会员进入直播间
	 * @param int $rin
	 * @param string $msg
	 * @return array code=>0, packdata
	 */
	public static function roomEnter($rin, $msg) {
		$proc   = new protocal(self::$socket);
		$pkdata = array('rin'=>$rin, 'msg'=>$msg);
		$pkdata = json_encode($pkdata);
		$nsize  = $proc->package(1, 3, $pkdata);
		if ($nsize != 0) {
			return array('code'=>1, 'packdata'=>'socket send error!');
		}
		return $proc->parse();
	}
	
	/**
	 * 直播间聊天发送消息
	 * @param int $reftype
	 * @param string $msg
	 * @return array code=>0, packdata
	 */
	public static function msgSend($reftype, $msg) {
		$proc   = new protocal(self::$socket);
		$pkdata = array('reftype'=>$reftype, 'msg'=>$msg);
		$pkdata = json_encode($pkdata);
		$nsize  = $proc->package(1, 4, $pkdata);
		if ($nsize != 0) {
			return array('code'=>1, 'packdata'=>'socket send error!');
		}
		return $proc->parse();
	}
	
	/**
	 * 在直播间中禁言某一个用户
	 * @param int $uin
	 * @param string $msg
	 * @return array code=>0, packdata
	 */
	public static function speakNot($uin, $msg) {
		$proc   = new protocal(self::$socket);
		$pkdata = array('uin'=>$uin, 'msg'=>$msg);
		$pkdata = json_encode($pkdata);
		$nsize  = $proc->package(1, 5, $pkdata);
		if ($nsize != 0) {
			return array('code'=>1, 'packdata'=>'socket send error!');
		}
		return $proc->parse();
	}
	
	/**
	 * 踢掉直播间某一个用户
	 * @param int $uin
	 * @param string $msg
	 * @return array code=>0, packdata
	 */
	public static function tioutUser($uin, $msg) {
		$proc   = new protocal(self::$socket);
		$pkdata = array('uin'=>$uin, 'msg'=>$msg);
		$pkdata = json_encode($pkdata);
		$nsize  = $proc->package(1, 6, $pkdata);
		if ($nsize != 0) {
			return array('code'=>1, 'packdata'=>'socket send error!');
		}
		return $proc->parse();
	}
	
	/**
	 * 获取当前用户所在直播间的用户uin
	 * @return array code=>0, packdata
	 */
	public static function members() {
		$proc   = new protocal(self::$socket);
		$pkdata = '';
		$nsize  = $proc->package(1, 7, $pkdata);
		if ($nsize != 0) {
			return array('code'=>1, 'packdata'=>'socket send error!');
		}
		return $proc->parse();
	}
	
	/**
	 * 生成客户端可连接的验证加密token
	 * @param int $uin
	 * @param string $ipstr
	 * @param string $vkey
	 * @return string
	 */
    public static function token($uin, $ipstr, $vkey) {
    	$szstr = sprintf('%s;%d', $ipstr, $uin);
    	$szstr = self::encrypt($szstr, $vkey);
    	return $szstr;
    }
}